package com.yh.controler;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.yh.dao.AdminDao;
import com.yh.model.Admin;
import com.yh.utils.FileId;

@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends ActionSupport {

	private String name;
	private String pwd;
	private static String head;   
	private String uploadFileName; 
	private File upload;
	
	@Autowired
	private Admin admin;
	@Autowired
	private AdminDao adminDaoImpl; //spring 整合,都要写借口类型,实现类被mapper.xml文件替代
	
	
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String execute(){
		return "success";
	}

	
	public String login(){
		String result = "fail";
		Map map = new HashMap();
		map.put("name", name);
		map.put("pwd", pwd);
		admin = adminDaoImpl.selectOne(map);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getSession().setAttribute("admin", admin);
		if( admin != null){
			result = "success";
		}
		return result;
	}
	
	
	 
	public void register1(){
		
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload"); 
		PrintWriter out = null;
		System.out.println("upheadFileName:"+uploadFileName.substring(uploadFileName.indexOf(".")+1));
		System.out.println("savePath:"+savePath);
		
		try {
			out = ServletActionContext.getResponse().getWriter();
			head = FileId.getFileId()+uploadFileName.substring(uploadFileName.lastIndexOf(".")); 
			FileUtils.copyFile(upload, new File(savePath,head)); 
			String json = "{\"cData\":\"1\",\"cMsg\":\"�ϴ��ɹ�\"}";
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
		
	}
	/**
	 * ע��
	 * @return
	 */
	public String register(){
		
		admin.setName(name);
		admin.setPwd(pwd);
		admin.setHead(head);
		if(adminDaoImpl.insertOne(admin)>0){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.getSession().setAttribute("admin", admin);
			return "success";
		}else{
			return "fail";
		}
		
	}
	
}
